package com.ph.website.home.service.impl;

import com.ph.website.home.base.common.CommonResult;
import com.ph.website.home.base.common.Ref;
import com.ph.website.home.entity.Address;
import com.ph.website.home.mapper.AddressMapper;
import com.ph.website.home.param.AddressParam;
import com.ph.website.home.service.IAddressService;
import com.ph.website.home.vo.AddressVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class AddressServiceImpl implements IAddressService {

    @Resource
    private AddressMapper addressMapper;

    /**获取公司地址
     *
     * 名称:  武汉普联东文数据服务有限责任公司
     * 地址:  武汉市江汉区江兴路17号欣欣中信科技孵化器A栋3层1室02号
     * 客服热线:    4000-365-108
     * 服务邮箱:    business@puldw.com
     * 经度:  114.239585
     * 纬度:  30.630097
     *
     * */
    public CommonResult get(){

        Address address = addressMapper.get();
        if(address==null){
            address = new Address();
            address.setName("武汉普联东文数据服务有限责任公司");
            address.setAddress("武汉市江汉区江兴路17号欣欣中信科技孵化器A栋3层1室02号");
            address.setTel("4000-365-108");
            address.setEmail("business@puldw.com");
            address.setLng(114.239585);
            address.setLat(30.630097);
            addressMapper.insert(address);
        }
        AddressVo.GetVo vo = new AddressVo.GetVo();
        vo.setName(address.getName());
        vo.setAddress(address.getAddress());
        vo.setTel(address.getTel());
        vo.setEmail(address.getEmail());
        vo.setLng(address.getLng());
        vo.setLat(address.getLat());
        return Ref.ok(vo);
    }


    /**设置公司地址*/
    public CommonResult set(AddressParam.SetParam param){
        Address address = addressMapper.get();
        if(address==null){
            address = new Address();
            address.setName(param.getName());
            address.setAddress(param.getAddress());
            address.setTel(param.getTel());
            address.setEmail(param.getEmail());
            address.setLng(param.getLng());
            address.setLat(param.getLat());
            addressMapper.insert(address);
        }else{
            address.setName(param.getName());
            address.setAddress(param.getAddress());
            address.setTel(param.getTel());
            address.setEmail(param.getEmail());
            address.setLng(param.getLng());
            address.setLat(param.getLat());
            addressMapper.updateById(address);
        }
        return Ref.ok();
    }
}
